package com.hcrazy.manchester.app.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.form.ForumSearchForm;
import com.hcrazy.manchester.app.pojo.BookMenu;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.Topic;
import com.hcrazy.manchester.app.services.BookMenuService;
import com.hcrazy.manchester.app.services.ForumService;
import com.hcrazy.manchester.app.services.TopicService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
public class AppointController {
	
	@Autowired
	private TopicService topicService;
	
	@Autowired
	private ForumService forumService;
	
	@Autowired
	private BookMenuService bookMenuService;
	
	@RequestMapping("/admin/forum/addAppoint")
	public String addAppoint(Model model){
		try {
			List<BookMenu> sorts = bookMenuService.getAllMenus(Constant.TYPE_FORUM_APPOINT,0);
			model.addAttribute("sorts", sorts);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
		Topic topic = new Topic();
		model.addAttribute("topic", topic);
		
		return "/admin/forum/editAppoint";
	}
	
	/**
	 * "约"管理
	 * @param model
	 */
	@RequestMapping("/admin/forum/appoint")
	public void topic(Model model){
		//根据查询参数获取分页对象：默认第一页、板块参数为约、查询字符串为空
		PageModel pageModel = getAllTopics(1, Constant.FORUM_TYPE_APPOINT_ID, "", "", null, null);
		model.addAttribute("pageModel", pageModel);
				
		//初始化查询对象
		model.addAttribute("searchForm", new ForumSearchForm());
	}
	
	@RequestMapping("/admin/forum/appoint/{topicId}")
	public String editTopic(Model model,@PathVariable long topicId) {
		Topic topic = null;
		try {
			List<BookMenu> sorts = bookMenuService.getAllMenus(Constant.TYPE_FORUM_APPOINT,0);
			model.addAttribute("sorts", sorts);
			
			topic = topicService.getTopicById(topicId);
			model.addAttribute("topic", topic);
			return "/admin/forum/editAppoint";
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return "/admin/failed";
		}
		
	}
	
	/**
	 * 约主题修改
	 * @param search
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/admin/forum/appoint",method=RequestMethod.POST)
	public String topic(@ModelAttribute("form") Topic topic,Model model){
		try {
			topic.setForumId(Constant.FORUM_TYPE_APPOINT_ID);
			if(topic.getTopicId() > 0){
				topicService.updateTopic(topic);
			}else{
				topicService.addTopic(topic);
			}
			
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/forum/appoint";
		
	}
	
	/**
	 * 发布约
	 * @param search
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/admin/forum/add_appoint",method=RequestMethod.POST)
	public String add_appoint(@ModelAttribute("form") Topic topic,Model model){
		try {
			topic.setForumId(Constant.FORUM_TYPE_APPOINT_ID);
			topic.setAuthor("admin");
			topicService.addTopic(topic);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/forum/appoint";
		
	}
	
	/**
	 * “约”查询
	 * @param search
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/admin/forum/appoint/search",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public String search(@ModelAttribute("form") ForumSearchForm search,Model model){
		
		//是否执行删除操作
		if(search.getForumId() > 0){
			try {
				topicService.delTopic(search.getForumId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置infoId
		search.setForumId(0);	
		
		PageModel pageModel = getAllTopics(search.getPageNum(), Constant.FORUM_TYPE_APPOINT_ID, search.getAuthors(), 
				search.getTitle(), search.getStartTime(), search.getEndTime());
		model.addAttribute("pageModel", pageModel);
		model.addAttribute("searchForm", search);
		return "/admin/forum/appoint";
	}
	
	/**
	 * 默认为获取论坛帖子
	 * @param pageNum
	 * @param forumId
	 * @param authors
	 * @param title
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	private PageModel getAllTopics(int pageNum,int forumId,
			String authors,String title,String startTime,String endTime){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		try {
			return topicService.getTopics(model, forumId, authors, title, startTime, endTime,Constant.TYPE_FORUM,0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	/**********************************约类型管理***********************************/
	
	@RequestMapping(value="/admin/forum/menu",method=RequestMethod.GET)
	public void menu(Model model,HttpServletRequest req){
		
	}
	
	/**
	 * 跳转到添加子分类页面
	 * 初始化数据
	 * @param menuParent
	 * @param model
	 */
	@RequestMapping(value="/admin/forum/addMenu",method=RequestMethod.GET)
	public void addMenu(@RequestParam int menuParent,Model model){
		//根据父分类id获取父分类对象
		try {
			BookMenu parentMenu = new BookMenu();
			if(menuParent == 0){
				parentMenu.setMenuId(0);
				parentMenu.setMenuName("无");
			}else{
				parentMenu = bookMenuService.getMenuById(menuParent);
			}
			model.addAttribute("parentMenu",parentMenu);
			BookMenu bookMenu = new BookMenu();
			model.addAttribute("bookMenu",bookMenu);
			
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
	}
	
	/**
	 * 跳转到编辑分类页面
	 * @param menuId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/admin/forum/editMenu",method=RequestMethod.GET)
	public String editMenu(@RequestParam int menuId,Model model){
		
		try {
			BookMenu bookMenu = bookMenuService.getMenuById(menuId);
			addMenu(bookMenu.getMenuParent(),model);
			
			model.addAttribute("bookMenu",bookMenu);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
		return "/admin/forum/addMenu";
	}
    
	/**
	 * 删除分类
	 * @param menuId
	 * @return
	 */
	@RequestMapping(value="/admin/forum/delMenu",method=RequestMethod.GET)
	public String delMenu(@RequestParam int menuId,Model model){
		try {
			String msg = bookMenuService.delBookMenu(menuId);
			if(null != msg){
				model.addAttribute("message", msg);
			}
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "redirect:/admin/forum/menu";
	}
	
	/**
	 * 提交分类信息
	 * @param menu
	 * @return
	 */
	@RequestMapping(value="/admin/forum/menu",method=RequestMethod.POST)
    public String addOrUpdateMenu(@ModelAttribute("form") BookMenu menu,
    		Model model){
		try {
			menu.setType(Constant.TYPE_FORUM_APPOINT);
			bookMenuService.addOrUpdateBookMenu(menu);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/forum/menu";
    }
	
	/**
	 * 获取全部分类（树状结构）
	 * @return
	 */
	@RequestMapping(value="/admin/forum/menus",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<BookMenu> getAllMenus(){
		
		try {
			return bookMenuService.getAllMenus(Constant.TYPE_FORUM_APPOINT,0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
}
